package com.hlw.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hlw.core.TreeTableModel;
import com.hlw.dao.MenuMapper;
import com.hlw.dao.RoleMapper;
import com.hlw.entity.Menu;
import com.hlw.entity.Role;
import com.hlw.entity.User;
import com.hlw.service.IMenuService;
import com.hlw.service.IRoleService;
import com.hlw.service.IUserService;

/**
 * <p>
 * 前端控制器
 * </p>
 * 
 * @author Mht
 * @since 2020-05-17
 */
@Controller
@RequestMapping("/role")
public class RoleController {
	@Resource
	private IRoleService roleService;

	@Resource
	private IUserService userService;
	@Resource
	private MenuMapper menuMapper;
	@Resource
	private IMenuService menuService;
	@Resource
	private RoleMapper roleMapper;

	/**
	 * 角色list列表页面
	 * @return
	 */
	@RequestMapping("/roleList")
	public String roleList(Model model) {
		//获取当前用户，只有角色为admin的才有权限曾删改查
	     User user = (User) SecurityUtils.getSubject().getPrincipal();
	    Integer rid= roleMapper.findRoleByUid(user.getId());
		Role role = roleService.selectById(rid);
		model.addAttribute("userRole", role.getName());
		return "/system/role/rolelist";

	}
	@RequestMapping("/addRole")
	public String addRole() {
		return "/system/role/role_add";

	}
	@RequestMapping("/saveRole")
	@ResponseBody
	public String saveRole(Role role) {
		
			roleService.insertOrUpdate(role);		
			return "success";
			
	
		
	}
	@RequestMapping(value="/editRole",method=RequestMethod.GET)
	public String editRole(Integer rid,Model model) {
		Role role = roleService.selectById(rid);
		model.addAttribute("role", role);
		return "/system/role/role_update";

	}
	
	
	@RequestMapping("/delRole")
	public String delRole(Integer rid,Model model) {
		Role role = roleService.selectById(rid);
		if(role.getName().equals("admin")&&role.getName().equals("user")){
			String tip = "该角色为系统默认角色，无法删除";
			return tip;
		}else{
			menuService.delById(rid);
		 	
			roleService.deleteById(rid);
			
		}		 
		return "/system/role/role_update";

	}
	
	/**
	 * 根据角色Id 查询角色权限
	 * 
	 * @param rid
	 * @return
	 */
	@RequestMapping("/setPer")
	public String setPer(Integer rid, Model model) {

		List<Menu> plist = menuMapper.findPermissionByRoleId(rid);// 获取用户权限
		List<Integer> perminsStrlist = new ArrayList<Integer>();// 角色的权限集合
		for (Menu uPermission : plist) {
			if (uPermission.getPid() != 0) {
				perminsStrlist.add(uPermission.getId());
			}
		}
		model.addAttribute("menuList", perminsStrlist);
		System.out.println(perminsStrlist);
		
		model.addAttribute("rid", rid);
		return "/system/role/role_set_per";

	}

	/**
	 * 根据保存角色
	 * 
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "setRole", method = RequestMethod.POST)
	@ResponseBody
	public String setRole(Integer uid, Integer rid) {
		Integer counts = roleService.findRoleByUid(uid);
		
		if (counts != null) {

			roleService.deleteRoleByUid(uid);
			roleService.inserRoleByRidOrUid(uid, rid);
			return "success";
		} else {
			roleService.inserRoleByRidOrUid(uid, rid);
			return "success";
		}

	}

	/**
	 * 查询所有角色
	 */
	@RequestMapping("/findRoleByUid")
	public String findRoleByUid(Integer uid, Model model) {
		/* 根据uid来查找角色id */
		Integer roleId = roleService.findRoleByUid(uid);
		User user = userService.selectById(uid);
		System.out.println(roleId);
		/* List<Role> roles = roleService.selectAll(); */
		model.addAttribute("user", user);
		model.addAttribute("uid", uid);
		model.addAttribute("roleId", roleId);
		return "/system/user/user_set_role";

	}

	/**
	 * 查询所有角色
	 */
	@RequestMapping("/findAll")
	@ResponseBody
	public TreeTableModel findAll() {

		List<Role> roles = roleService.selectAll();

		return new TreeTableModel(roles);

	}

	/**
	 * 角色设置权限。权限可为null
	 */
	@RequestMapping("/updatePer")
	@ResponseBody
	public String updatePer(Integer[] arr, Integer rid) {
		if(arr!=null){
		List<Menu> menus = menuMapper.findPermissionByRoleId(rid);
		if (menus.size()>0 ) {
			menuService.delById(rid);
			for (Integer item : arr) {
				menuService.addPerByRid(rid, item);
			}
			return "success";
		} else {
			for (Integer item : arr) {
				menuService.addPerByRid(rid, item);
			}
			return "success";
		}
		}else{
			menuService.delById(rid);
			return "success";
			
		}

	}
	
	

}
